<?php

class MonitorController extends Controller
{
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	public function accessRules(){
	    return array(
	        array('allow', // allow authenticated users to access all actions
				'users'=>array('@'),
			),
	        array('deny', // deny anything else
	              'users'=>array('*'),
	        ),
	    );
	}

    public function actionIndex()
    {   
            $this -> render('index',array(	

            ));
    }

    public function actionSearch()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $monitor = new Monitor();

            $data = $monitor -> search();

            echo json_encode(array("results" => $data[0], "total" => $data[1]));
            return;
        }
    }

    public function actionCreate()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $monitor = new Monitor();
            
            $monitor -> domain = $_POST['domain'];
            $monitor -> note = $_POST['note'];
            $monitor -> alexaRank = $_POST['alexaRank'];
            
            if($monitor -> save())
            {
                echo json_encode(array("status" => 'success'));
            }
            else
            {
                echo json_encode(array("status" => 'failure'));
            }

            return;
        }
    }
    
    public function actionUpdate()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $monitor = Monitor::model() -> findByPk($_POST['id']);
            
            $monitor -> domain = $_POST['domain'];
            $monitor -> note = $_POST['note'];
            $monitor -> alexaRank = $_POST['alexaRank'];
            
            if($monitor -> save())
            {
                echo json_encode(array("status" => 'success'));
            }
            else
            {
                echo json_encode(array("status" => 'failure'));
            }
        }
    }
    
    public function actionUpdateRate()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $monitor = Monitor::model() -> findByPk($_POST['id']);
            $monitor -> rate = $_POST['rate'];

            if($monitor -> save())
            {
                echo json_encode(array("status" => 'success'));
            }
            else
            {
                echo json_encode(array("status" => 'failure'));
            }
        }
    }
    
    public function actionDelete()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $monitor = new Monitor();
             if ($monitor -> deleteMonitor($_POST['id'])) {
                echo json_encode(array("status" => 'success'));
            } else {
                echo json_encode(array("status" => 'failure'));
            }
        }
        
        return;
    }
    
    public function actionGetNote()
    {
        $id = $_GET['id'];
        $item = Monitor::model() -> findByPk($id);
        
        echo $item -> note;
    }
    
    public function actionGetItem()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $id = $_POST['id'];
            $monitor = new Monitor();
            $item = $monitor -> getItem($id);
            
            echo json_encode($item);
        }
        
        return;
    }
}